* This code replicates the main results in
* Why do firms borrow from nonbanks? (forthcoming RFS)
* by Sergey Chernenko, Isil Erel, and Robert Prilmeier
*
* The main data set is nonbank_lending_pseudodata.dta
* This is a pseudo data set that illustrates the format of the original data set
	

/****************************************************************************************************
Figure 1: Fraction of loans obtained from nonbanks by EBITDA bin
****************************************************************************************************/
use nonbank_lending_pseudodata, clear
local g 1

gen ebitdabucket = 1 if ~mi(ebitda)
sum ebitda
local bucketmin = r(min)
local count = 1
foreach i of numlist -25 -10 -5 -2.5 0 2.5 5 10 15 20 25 30 40 50 75 100 125 150 200 {
	replace ebitdabucket = `count' if ebitda > `bucketmin' & ebitda <= `i'
	local count = `count' + 1
	local bucketmin = `i'
}
replace ebitdabucket = `count' if ebitda > `bucketmin' & ebitda < .

preserve
statsby nonbankperc = r(mean), clear nodots by(ebitdabucket): su nonbank
generate ebitdabucket1 = ebitdabucket - 6
replace ebitdabucket1 = 0 if ebitdabucket>=6
generate ebitdabucket2 = ebitdabucket - 6
replace ebitdabucket2 = 0 if ebitdabucket<6
generate int1 = 1
replace int1 = 0 if ebitdabucket >=6
generate int2 = 1
replace int2 = 0 if ebitdabucket<6
reg nonbankperc int1 int2 ebitdabucket1 ebitdabucket2, hascons
predict yhat
twoway (scatter nonbankperc ebitdabucket) (line yhat ebitdabucket if ebitdabucket <6, sort) (line yhat ebitdabucket if ebitdabucket >=6, sort), name(g`g++', replace) xtitle("EBITDA bins") ytitle("Fraction of loans obtained from nonbanks") legend(off)
graph export figEBITDAbucketsdiscontinuity.pdf , as(pdf) replace
restore
	
	

/****************************************************************************************************
Figure 2: Histogram of trailing twelve months EBITDA
****************************************************************************************************/
histogram ebitda if inrange(ebitda, -25, 25), start(-25) width(0.5)
graph export figHistTTMEBITDA.pdf, as(pdf) replace


/****************************************************************************************************
Table 1: Number of loans originated, lender types and DealScan match rates
****************************************************************************************************/
use nonbank_lending_pseudodata, clear


*Panel A: Loans originated per year
table originationyear, c(freq mean nonbank)
su nonbank


*Panel B: Lender types and DealScan match rates
tab dsmatch if nonbank == 0
tab lendertype dsmatch if nonbank == 1, row
tab lendertype if nonbank == 1


/****************************************************************************************************
Table 2: Summary statistics for bank vs. nonbank loans
****************************************************************************************************/
capture program drop univariatetests
program univariatetests, eclass
	syntax varlist if, byvar(varname)
	quietly count if `byvar' != 0 & `byvar' != 1
	quietly gen ranksumvar = 1 - `byvar'
	local a = r(N)
	if `a' > 0 {
		display as error "`byvar' is not an indicator variable. Variables with values other than zero and one not allowed."
	}

	foreach var of local varlist {
		quietly su `var' `if' & `byvar' == 1
		matrix group1obs = nullmat(group1obs),r(N)

		quietly su `var' `if' & `byvar' == 1, detail
		local mediantemp1 = r(p50)
		matrix group1mean = nullmat(group1mean),r(mean)
		matrix group1median = nullmat(group1median),r(p50)
		matrix group1sd = nullmat(group1sd),r(sd)

		quietly su `var' `if' & `byvar' == 0
		matrix group2obs = nullmat(group2obs),r(N)

		quietly su `var' `if' & `byvar' == 0, detail
		local mediantemp2 = r(p50)
		matrix group2mean = nullmat(group2mean),r(mean)
		matrix group2median = nullmat(group2median),r(p50)
		matrix group2sd = nullmat(group2sd),r(sd)


		quietly ttest `var' `if', by(`byvar') unequal
		matrix groupdiff = nullmat(groupdiff),r(mu_2)-r(mu_1)
		matrix difftstat = nullmat(difftstat),-r(t)
		matrix diffpval = nullmat(diffpval),r(p)

		quietly ranksum `var' `if', by(ranksumvar)
		matrix mediandiff = nullmat(mediandiff),`mediantemp1' - `mediantemp2'
		matrix medianz = nullmat(medianz),r(z)
		matrix medianpval = nullmat(medianpval),(1-normal(abs(r(z))))*2
	}

	quietly count `if' & `byvar' != .
	scalar nObs = r(N)
	quietly count `if' & `byvar' == 1
	scalar firstn = r(N)
	quietly count `if' & `byvar' == 0
	scalar secondn = r(N)

	foreach mat in group1obs group1mean group1median group1sd group2obs group2mean group2median group2sd groupdiff difftstat diffpval mediandiff medianz medianpval {
		mat coln `mat' = `varlist'
	}


	tempname foo bar
	mat `foo' = group1mean*0
	mat `bar' = `foo''*`foo'

	eret post `foo' `bar'
	eret local cmd "univariate"

	foreach mat in group1obs group1mean group1median group1sd group2obs group2mean group2median group2sd groupdiff difftstat diffpval mediandiff medianz medianpval {
		eret mat `mat' = `mat'
	}

	eret scalar nObs = nObs
	eret scalar firstn = firstn
	eret scalar secondn = secondn
	drop ranksumvar
end

use nonbank_lending_pseudodata, clear
gen sample = 1
eststo clear
univariatetests realassets ebitda negebitda de_pos profitability leverage leverageincrease assetgrowth_abs mkt_bk research tangibility currentratio firmage growth vol_200_20_all ret_200_20_all trancheamount mat fixedrateloan interestrate senior secondlien security performancepricing upfrontfee_bp annualfee_bp financialcovenants warrants convertible if sample==1, byvar(nonbank)

esttab using pseudodata_output\univariatetests.rtf, cells("group1obs(fmt(0) label(Nonbank observations)) group1mean(fmt(2) label(Nonbank)) group1median(fmt(2) label(Nonbank med)) group1sd(fmt(2) label(Nonbank sd)) group2obs(fmt(0) label(Bank loans)) group2mean(fmt(2) label(Bank loans) star pvalue(diffpval)) group2median(fmt(2) label(Bank med) star pvalue(medianpval)) group2sd(fmt(2) label(Bank sd))")  stats(nObs firstn secondn, fmt(0) labels("Total observations") layout("@ @ @")) starlevels(* 0.1 ** 0.05 *** 0.01) nonumbers label modelwidth(12) varwidth(20) replace



*Table 3: Probability of borrowing from a nonbank lender
use nonbank_lending_pseudodata, clear
eststo clear
eststo: xi: reg nonbank lnassets negebitda de_pos de6 ebitda leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage i.originationyear i.industry12 , cluster(cik)
eststo: xi: reg nonbank lnassets negebitda de_pos de6 ebitda leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.industry12 , cluster(cik)
eststo: xi: reg nonbank lnassets negebitda de_pos de6 ebitda negebitda_X_abl abl de_pos_X_g2013 de6_X_g2013 g2013 leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.industry12 , cluster(cik)
test negebitda + negebitda_X_abl ==0
eststo: xi: reg nonbank lnassets negebitda de_pos de6 ebitda negebitda_X_abl abl de_pos_X_g2013 de6_X_g2013 g2013 leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage i.originationyear i.cik, cluster(cik)
eststo: xi: reg nonbank lnassets negebitda de_pos de6 ebitda negebitda_X_abl abl de_pos_X_g2013 de6_X_g2013 g2013 leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.cik , cluster(cik)
esttab using pseudodata_output\nonbanklinearprob.rtf, compress b(2) stats(N r2, fmt(0 2) layout("@") labels("Observations")) indicate("Year effects = *Ioriginat*" "Industry effects = *Iindus*" "Borrower effects = *Icik*", labels(Yes No)) starlevels(* .10 ** .05 *** .01) label eqlabels(none) order(negebitda de_pos de6 ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all) varwidth(15) modelwidth(7) nomtitle nolegend replace

	
*Table 4: Probability of borrowing from a bank regulated by the OCC
use nonbank_lending_pseudodata, clear
eststo clear
eststo: xi: reg occ negebitda de_pos de6 ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.industry12  if lendertype == 2 & foreignbank == 0, cluster(cik)
eststo: xi: reg occ negebitda de_pos de6 ebitda negebitda_X_abl abl de_pos_X_g2013 de6_X_g2013 negebitda_X_g2013 g2013 lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.industry12  if lendertype == 2 & foreignbank == 0, cluster(cik)
eststo: xi: reg occ negebitda de_pos de6 ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.cik  if lendertype == 2 & foreignbank == 0, cluster(cik)
eststo: xi: reg occ negebitda de_pos de6 ebitda negebitda_X_abl abl de_pos_X_g2013 de6_X_g2013 negebitda_X_g2013 g2013 lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.cik  if lendertype == 2 & foreignbank == 0, cluster(cik)
esttab using pseudodata_output\occprob.rtf, compress b(2) stats(N r2, fmt(0 2) layout("@") labels("Observations")) indicate("Year effects = *Ioriginat*" "Industry effects = *Iindus*" "Borrower effects = *Icik*", labels(Yes No)) starlevels(* .10 ** .05 *** .01) label eqlabels(none) order(negebitda de_pos de6 ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all) varwidth(15) modelwidth(7) nomtitle nolegend replace


*Table 5: Nonbank lending and leniency of regulators supervising local banks
use nonbank_lending_pseudodata, clear
eststo clear
eststo: xi: reg nonbank negebitda negebitda_X_socc socc de6 de6_X_socc de_pos de_pos_X_socc ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.industry12 if abl == 0, cluster(cik)
eststo: xi: reg nonbank negebitda negebitda_X_socc socc de6 de6_X_socc de_pos de_pos_X_socc ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all unempr d_pcpi ln_pcpi ln_pop tier1_lev_all ln_deposits i.originationyear i.industry12 if abl == 0, cluster(cik)
eststo: xi: reg nonbank negebitda negebitda_X_socc socc de6 de6_X_socc de_pos de_pos_X_socc ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.industry12 if abl == 1, cluster(cik)
eststo: xi: reg nonbank negebitda negebitda_X_socc socc de6 de6_X_socc de_pos de_pos_X_socc ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all unempr d_pcpi ln_pcpi ln_pop tier1_lev_all ln_deposits i.originationyear i.industry12 if abl == 1, cluster(cik)
qui: xi: reg nonbank negebitda negebitda_X_socc socc de6 de6_X_socc de_pos ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all unempr d_pcpi ln_pcpi ln_pop tier1_lev_all ln_deposits i.originationyear i.industry12, cluster(cik)
egen byte large_firm = cut(sale) if e(sample), group(2)
eststo: xi: reg nonbank negebitda negebitda_X_socc socc de6 de6_X_socc de_pos de_pos_X_socc ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.industry12 if large_firm == 0, cluster(cik)
eststo: xi: reg nonbank negebitda negebitda_X_socc socc de6 de6_X_socc de_pos de_pos_X_socc ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all unempr d_pcpi ln_pcpi ln_pop tier1_lev_all ln_deposits i.originationyear i.industry12 if large_firm == 0, cluster(cik)
eststo: xi: reg nonbank negebitda negebitda_X_socc socc de6 de6_X_socc de_pos de_pos_X_socc ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.industry12 if large_firm == 1, cluster(cik)
eststo: xi: reg nonbank negebitda negebitda_X_socc socc de6 de6_X_socc de_pos de_pos_X_socc ebitda lnassets leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all unempr d_pcpi ln_pcpi ln_pop tier1_lev_all ln_deposits i.originationyear i.industry12 if large_firm == 1, cluster(cik)
esttab using pseudodata_output\occshare.rtf, compress b(2) stats(N r2, fmt(0 2) layout("@") labels("Observations")) indicate("Year effects = *Ioriginat*" "Industry effects = *Iindus*", labels(Yes No)) starlevels(* .10 ** .05 *** .01) label eqlabels(none) order(negebitda negebitda_X_socc socc de6 de6_X_socc de_pos de_pos_X_socc unempr d_pcpi ln_pcpi ln_pop tier1_lev_all ln_deposits) varwidth(15) modelwidth(7) nomtitle nolegend replace


*Table 6: Initial interest rate charged on bank versus nonbank loans
use nonbank_lending_pseudodata, clear
qui xi : reg interestrate nonbank negebitda de_pos de6 ln_distance same_fips negebitda_X_nonbank de6_X_nonbank de_pos_X_nonbank negebitda_X_g2013 de6_X_g2013 de_pos_X_g2013 g2013 lnamount performancepricing warrants convertible financialcovenants security secondlien mat fixedrateloan senior lnassets ebitda negebitda leverage leverageincrease assetgrowth_abs  research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all  i.industry12 i.originationyear ,  cluster(cik)
gen byte esample = e(sample)
eststo clear
eststo: xi: reg interestrate nonbank i.industry12 i.originationyear if esample, cluster(cik)
eststo: xi: reg interestrate nonbank negebitda de_pos de6 lnassets ebitda leverage leverageincrease assetgrowth_abs  research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.industry12 i.originationyear if esample, cluster(cik)
eststo: xi: reg interestrate nonbank negebitda de_pos de6 lnassets ebitda leverage leverageincrease assetgrowth_abs  research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all lnamount performancepricing warrants convertible financialcovenants security secondlien mat fixedrateloan senior i.industry12 i.originationyear if esample, cluster(cik)
eststo: xi: reg interestrate nonbank negebitda de_pos de6 ln_distance same_fips lnassets ebitda leverage leverageincrease assetgrowth_abs  research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all lnamount performancepricing warrants convertible financialcovenants security secondlien mat fixedrateloan senior i.industry12 i.originationyear if esample, cluster(cik)
eststo: xi: reg interestrate nonbank negebitda de_pos de6 negebitda_X_nonbank de6_X_nonbank de_pos_X_nonbank negebitda_X_g2013 de6_X_g2013 de_pos_X_g2013 g2013  ln_distance same_fips lnassets ebitda leverage leverageincrease assetgrowth_abs  research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all lnamount performancepricing warrants convertible financialcovenants security secondlien mat fixedrateloan senior i.industry12 i.originationyear if esample, cluster(cik)
eststo: xi: reg interestrate nonbank negebitda de_pos de6 ln_distance same_fips lnassets ebitda leverage leverageincrease assetgrowth_abs  research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all lnamount performancepricing warrants convertible financialcovenants security secondlien mat fixedrateloan senior i.cik i.originationyear if esample, cluster(cik)
eststo: xi: reg interestrate nonbank negebitda de_pos de6 negebitda_X_nonbank de6_X_nonbank de_pos_X_nonbank negebitda_X_g2013 de6_X_g2013 de_pos_X_g2013 g2013  ln_distance same_fips lnassets ebitda leverage leverageincrease assetgrowth_abs  research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all lnamount performancepricing warrants convertible financialcovenants security secondlien mat fixedrateloan senior i.cik i.originationyear if esample, cluster(cik)
esttab using pseudodata_output\interestrate.rtf, compress b(2) stats(N r2, fmt(0 2) layout("@") labels("Observations")) indicate("Year effects = *Ioriginat*" "Industry effects = *Iindus*" "Borrower effects = *Icik*", labels(Yes No)) starlevels(* .10 ** .05 *** .01) label eqlabels(none)  varwidth(15) modelwidth(7) nomtitle nolegend replace
drop esample


*Table 7: Non-price terms of bank versus nonbank loans
use nonbank_lending_pseudodata, clear
eststo clear
eststo: xi: reg lnamount nonbank lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.industry12, cluster(cik)
eststo: xi: reg lnamount nonbank lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.cik, cluster(cik)
eststo: xi: reg mat nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.industry12, cluster(cik)
eststo: xi: reg mat nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.cik, cluster(cik)
eststo: xi: reg senior nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.industry12, cluster(cik)
eststo: xi: reg senior nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.cik, cluster(cik)
eststo: xi: reg security nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.industry12, cluster(cik)
eststo: xi: reg security nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.cik, cluster(cik)
esttab using pseudodata_output\nonpriceterms1.rtf, compress b(2) stats(N r2, fmt(0 2) layout("@") labels("Observations")) indicate("Year effects = *Ioriginat*" "Industry effects = *Iindu*" "Firm effects = *Icik*", labels(Yes No)) starlevels(* .10 ** .05 *** .01) label title(Nonpriceterms) varwidth(15) modelwidth(6) depvars nolegend replace nogaps

eststo clear
eststo: xi: reg financialcovenants nonbank lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.industry12, cluster(cik)
eststo: xi: reg financialcovenants nonbank lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.cik, cluster(cik)
eststo: xi: reg performancepricing nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.industry12 if fixedrateloan==0, cluster(cik)
eststo: xi: reg performancepricing nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.cik if fixedrateloan==0, cluster(cik)
eststo: xi: reg warrants nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.industry12, cluster(cik)
eststo: xi: reg warrants nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.cik, cluster(cik)
eststo: xi: reg convertible nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.industry12, cluster(cik)
eststo: xi: reg convertible nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.cik, cluster(cik)
esttab using pseudodata_output\nonpriceterms2.rtf, compress b(2) stats(N r2, fmt(0 2) layout("@") labels("Observations")) indicate("Year effects = *Ioriginat*" "Industry effects = *Iindu*" "Firm effects = *Icik*", labels(Yes No)) starlevels(* .10 ** .05 *** .01) label title(Nonpriceterms) varwidth(15) modelwidth(6) depvars nolegend replace nogaps

eststo clear
eststo: xi: reg fixedrateloan nonbank lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.industry12, cluster(cik)
eststo: xi: reg fixedrateloan nonbank lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.cik, cluster(cik)
eststo: xi: reg upfrontfee_bp nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.industry12, cluster(cik)
eststo: xi: reg upfrontfee_bp nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.cik, cluster(cik)
eststo: xi: reg annualfee_bp nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.industry12, cluster(cik)
eststo: xi: reg annualfee_bp nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.cik, cluster(cik)
eststo: xi: reg secondlien nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.industry12, cluster(cik)
eststo: xi: reg secondlien nonbank  lnassets ebitda de_pos de6 negebitda leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage i.originationyear i.cik, cluster(cik)
esttab using pseudodata_output\nonpriceterms3.rtf, compress b(2) stats(N r2, fmt(0 2) layout("@") labels("Observations")) indicate("Year effects = *Ioriginat*" "Industry effects = *Iindu*" "Firm effects = *Icik*", labels(Yes No)) starlevels(* .10 ** .05 *** .01) label title(Nonpriceterms) varwidth(15) modelwidth(6) depvars nolegend replace nogaps



*Table 8: Fuzzy RDD using the coverage error-rate-optimal bandwidth selector
use nonbank_lending_pseudodata, clear
keep if abl == 0

capture program drop rddoutput
program rddoutput, eclass
	syntax varlist [if]

	*Not enough observations in industry 7, so have to reclassify them as "other" to avoid invertibility problem
		gen industry12new = industry12
		replace industry12new = 11 if industry12==7
	*Go through list of output variables
	foreach var of local varlist {
	*Estimate RDD. 
	*For performance pricing, we need to drop fixed rate loans since performance pricing is a feature of floating rate loans
	if "`var'" == "performancepricing" {
		xi: rdrobust `var' ebitda `if' & fixedrateloan==0, c(0) p(1) q(2) deriv(0) fuzzy(nonbank) kernel(triangular) bwselect(cerrd) covs($rddcovs)  vce(cluster cik)
	}
	else{
		xi: rdrobust `var' ebitda `if', c(0) p(1) q(2) deriv(0) fuzzy(nonbank) kernel(triangular) bwselect(cerrd) covs($rddcovs)  vce(cluster cik)
	}
	*Extract parameters that we want to see in the table
	matrix bandwidth = nullmat(bandwidth),e(h_l)
	matrix leftobs = nullmat(leftobs),e(N_h_l)
	matrix rightobs = nullmat(rightobs),e(N_h_r)
	matrix firststagecoeff = nullmat(firststagecoeff), e(tau_T_cl)
	matrix firststagez = nullmat(firststagez),(e(tau_T_bc)/e(se_tau_T_rb))
	matrix firststagep = nullmat(firststagep),(2*normal(-abs((e(tau_T_bc)/e(se_tau_T_rb)))))
	matrix secondstagecoeff = nullmat(secondstagecoeff),e(tau_cl)
	matrix secondstagez = nullmat(secondstagez),(e(tau_bc)/e(se_tau_rb))
	matrix secondstagep = nullmat(secondstagep),(2*normal(-abs((e(tau_bc)/e(se_tau_rb)))))
	}

	foreach mat in bandwidth leftobs rightobs firststagecoeff firststagez firststagep secondstagecoeff secondstagez secondstagep {
		mat coln `mat' = `varlist'
	}


	tempname foo bar
	mat `foo' = bandwidth*0
	mat `bar' = `foo''*`foo'

	eret post `foo' `bar'
	eret local cmd "rddoutput"

	foreach mat in bandwidth leftobs rightobs firststagecoeff firststagez firststagep secondstagecoeff secondstagez secondstagep {
		eret mat `mat' = `mat'
	}

drop industry12new

end

global rddcovs lnassets leverage mkt_bk growth vol_200_20_all ret_200_20_all research tangibility currentratio lnfirmage  i.originationyear i.industry12new

eststo clear
xi: rddoutput interestrate lnamount mat senior security secondlien financialcovenants performancepricing warrants convertible fixedrateloan upfrontfee annualfee bankrupt_tplusfive profitability_chg_tplusthree ret3yr
esttab using pseudodata_output\rddoutput.rtf, cells("bandwidth(fmt(2) label(Bandwidth)) leftobs(fmt(0)) rightobs(fmt(0)) firststagecoeff(fmt(2) label(1st stage) star pval(firststagep)) secondstagecoeff(fmt(2) label(2nd stage) star pval(secondstagep))" ". . . firststagez(fmt(2) par) secondstagez(fmt(2) par)") starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers label modelwidth(8) varwidth(20) replace
eststo clear



*Table 9: Probability of bankruptcy for bank versus nonbank loans
use nonbank_lending_pseudodata, clear
eststo clear
eststo: xi: reg bankrupt_tplusfive nonbank i.originationyear i.industry12, cluster(cik)
eststo: xi: reg bankrupt_tplusfive nonbank lnassets ebitda negebitda leverage leverageincrease assetgrowth_abs tangibility currentratio research lnfirmage i.originationyear i.industry12, cluster(cik)
eststo: xi: reg bankrupt_tplusfive nonbank lnassets ebitda negebitda leverage leverageincrease assetgrowth_abs research lnfirmage  mkt_bk growth vol_200_20_all ret_200_20_all  tangibility currentratio i.originationyear i.industry12, cluster(cik)
eststo: xi: reg bankrupt_tplusfive nonbank lnassets ebitda negebitda leverage leverageincrease assetgrowth_abs research lnfirmage  mkt_bk growth vol_200_20_all ret_200_20_all  tangibility currentratio ln_distance same_fips i.originationyear i.industry12, cluster(cik)
eststo: xi: reg bankrupt_tplusfive nonbank lnassets ebitda negebitda leverage leverageincrease assetgrowth_abs research lnfirmage  mkt_bk growth vol_200_20_all ret_200_20_all  tangibility currentratio i.originationyear i.industry12 if senior==1 & security ==1 & secondlien == 0 & warrants == 0 & convertible == 0 & financialcovenants == 1 & fixedrateloan==0, cluster(cik)
eststo: xi: reg bankrupt_tplusfive nonbank lnassets ebitda negebitda leverage leverageincrease assetgrowth_abs research lnfirmage  mkt_bk growth vol_200_20_all ret_200_20_all  tangibility currentratio ln_distance same_fips i.originationyear i.industry12 if senior==1 & security ==1 & secondlien == 0 & warrants == 0 & convertible == 0 & financialcovenants == 1 & fixedrateloan==0, cluster(cik)
esttab using pseudodata_output\bankruptcy_fiveyear.rtf, compress b(3) stats(N r2, fmt(0 2) layout("@") labels("Observations")) indicate("Year effects = *Ioriginat*" "Industry effects = *Iindu*", labels(Yes No)) starlevels(* .10 ** .05 *** .01) label title(Bankruptcy) varwidth(15) modelwidth(8) depvars nolegend replace



*Appendix C: Multinomial logit regression for borrowing from a specific type of nonbank lender 
use nonbank_lending_pseudodata, clear
*Generate lender cluster variable
gen lendercluster = .
*Banks are cluster number 1
replace lendercluster = 1 if lendertype == 2
*FCOs and Bank FCOs are cluster number 2
replace lendercluster = 2 if inlist(lendertype,3,4)
*Investment banks are cluster number 3
replace lendercluster = 3 if lendertype == 7
*Hedge Funds, Investment Managers, PE/VCs and BDCs are cluster number 4
replace lendercluster = 4 if inlist(lendertype,1,5,8,10)
*Insurance companies are cluster number 5
replace lendercluster = 5 if lendertype == 6
*Other collective are dropped, so we simply leave lendercluster missing for them

eststo clear
eststo: xi: mlogit lendercluster lnassets negebitda ebitda leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.industry12, cluster(cik) rrr technique(bhhh)
esttab using pseudodata_output\nonbankmlogit.rtf, eform compress b(2) stats(N, fmt(0) layout("@") labels("Observations")) indicate("Year effects = *Ioriginat*" "Industry effects = *Iindus*", labels(Yes No)) starlevels(* .10 ** .05 *** .01) label eqlabels(none) title(Multinomial logit) order(lnassets ebitda negebitda leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth ) varwidth(15) modelwidth(6) nomtitle unstack noomitted nolegend replace nogaps
tab lendercluster if e(sample)

*Alternative lender cluster variable that splits FCOs and Bank FCOs
gen lendercluster_fcovsbankfco = .
*Banks are cluster number 1
replace lendercluster_fcovsbankfco = 1 if lendertype == 2
*Bank FCOs are cluster number 2
replace lendercluster_fcovsbankfco = 2 if lendertype == 3
*FCOs are cluster number 3
replace lendercluster_fcovsbankfco = 3 if lendertype == 4
*Investment banks are cluster number 4
replace lendercluster_fcovsbankfco = 4 if lendertype == 7
*Hedge Funds, Investment Managers, PE/VCs and BDCs are cluster number 5
replace lendercluster_fcovsbankfco = 5 if inlist(lendertype,1,5,8,10)
*Insurance companies are cluster number 5
replace lendercluster_fcovsbankfco = 6 if lendertype == 6
*Other collective are dropped, so we simply leave lendercluster missing for them

eststo clear
eststo: xi: mlogit lendercluster_fcovsbankfco lnassets negebitda ebitda leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth vol_200_20_all ret_200_20_all  i.originationyear i.industry12, cluster(cik) rrr technique(bhhh)
esttab using pseudodata_output\nonbankmlogit_fcovsbankfco.rtf, eform compress b(2) stats(N, fmt(0) layout("@") labels("Observations")) indicate("Year effects = *Ioriginat*" "Industry effects = *Iindus*", labels(Yes No)) starlevels(* .10 ** .05 *** .01) label eqlabels(none) title(Multinomial logit) order(lnassets ebitda negebitda leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth ) varwidth(15) modelwidth(6) nomtitle unstack noomitted nolegend replace nogaps
tab lendercluster_fcovsbankfco if e(sample)

*Alternative lender cluster variable that splits alternative investment vehicles into BDC/PE/VC vs HF/IM
gen lendercluster_altsplit = .
*Banks are cluster number 1
replace lendercluster_altsplit = 1 if lendertype == 2
*FCOs and Bank FCOs are cluster number 2
replace lendercluster_altsplit = 2 if inlist(lendertype,3,4)
*Investment banks are cluster number 3
replace lendercluster_altsplit = 3 if lendertype == 7
*PE/VCs and BDCs are cluster number 4
replace lendercluster_altsplit = 4 if inlist(lendertype,1,10)
*Hedge Funds, Investment Managers are cluster number 5
replace lendercluster_altsplit = 5 if inlist(lendertype,5,8)
*Insurance companies are cluster number 6
replace lendercluster_altsplit = 6 if lendertype == 6
*Other collective are dropped, so we simply leave lendercluster missing for them

eststo clear
eststo: xi: mlogit lendercluster_altsplit lnassets negebitda ebitda leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage  mkt_bk growth vol_200_20_all ret_200_20_all i.originationyear i.industry12, cluster(cik) rrr technique(bhhh)
esttab using pseudodata_output\nonbankmlogit_altsplit.rtf, eform compress b(2) stats(N, fmt(0) layout("@") labels("Observations")) indicate("Year effects = *Ioriginat*" "Industry effects = *Iindus*", labels(Yes No)) starlevels(* .10 ** .05 *** .01) label eqlabels(none) title(Multinomial logit) order(lnassets ebitda negebitda leverage leverageincrease assetgrowth_abs research tangibility currentratio lnfirmage mkt_bk growth) varwidth(15) modelwidth(6) nomtitle unstack noomitted nolegend replace nogaps
tab lendercluster_altsplit if e(sample)
